package com.jcnet.bizaia;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import com.actionbarsherlock.view.Menu;
import com.jcnet.bizaia.bean.SPSetting;
import com.jcnet.bizaia.bean.SearchBean;
import com.jcnet.bizaia.bean.VoiceSearchBean;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.xstream.XmlCacheHelper;

public class VoiceSearchActivity extends JCSimpleSherlockActivity implements   OnClickListener{

	private XmlCacheHelper cacheHelper ;

	private DataStorageHelper dataStorageHelper;

	public static final int RESULT_CODE="VoiceSearchActivity".hashCode();
	
	private int mark = -1;
	
	
	private BroadcastReceiver broadcastReceiver;
	
	public static final String DISS_BISNUESS_RECEIVER = "DISS_BISNUESS_RECEIVER_VoiceSearchActivity";
	
	
	private EditText editText;
	
	
	private VoiceSearchBean voiceSearchBean;
	
	private TextView categoryTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		cacheHelper = new XmlCacheHelper(this);
		dataStorageHelper = new DataStorageHelper(this, SPSetting.SETTING_TABLE_NAME);
		setContentView(R.layout.voice_search);
		super.jcTitle = getString(R.string.search);
		super.jcSubtitle = getString(R.string.voiceDetail);
		super.initActionBar();
		categoryTextView = (TextView)findViewById(R.id.adressTitle_Diss);
		editText = (EditText)findViewById(R.id.EditText_Search);
		editText.setText("  ");
		voiceSearchBean  = new VoiceSearchBean("", "", -1, -1+"");
		broadcastReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if(intent.getAction()==DISS_BISNUESS_RECEIVER){
					
					 voiceSearchBean = (VoiceSearchBean) intent.getSerializableExtra("categoryInfo");
					 
					 categoryTextView.setText(voiceSearchBean.getVoiceSearchName());
					
				}
				
			}
		};
		
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(DISS_BISNUESS_RECEIVER);
		registerReceiver(broadcastReceiver, intentFilter);
		
	}


	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		//getSupportMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		unregisterReceiver(broadcastReceiver);
	}
	


	
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		List<SearchBean> searchBeans = new ArrayList<SearchBean>();   //传递给下个页面的数据
		switch (v.getId()) {
	
		case R.id.adressLin:
			
			startActivity(new Intent(VoiceSearchActivity.this, VoiceCategoryActivityForSearch.class));
			
			break;
			
			
		case R.id.dataLin:
			new DatePickerDialog(VoiceSearchActivity.this, new OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					
					String dateString = year+"-"+monthOfYear+"-"+dayOfMonth;
					
					((TextView)findViewById(R.id.DataTitle_Diss)).setText(dateString);
					
					voiceSearchBean.setVoiceSearchDate(dateString);
					
				}
			}, new Date().getYear(), new Date().getMonth(), new Date().getDay()).show();
			 
			break;

	
			
		case R.id.discussSearchButton:   //点击提交
			
			voiceSearchBean.setVoiceSearchTitle(editText.getText().toString().trim());
			Intent intent2 = new Intent(VoiceSearchActivity.this, SearchResultActivity.class);
			intent2.putExtra("voiceSearchItem", voiceSearchBean);
			startActivity(intent2);
			break;
			
			
		case R.id.vodClearSearchButton:
			editText.setText("  ");
			categoryTextView.setText(getString(R.string.allCategory));
			((TextView)findViewById(R.id.DataTitle_Diss)).setText(getString(R.string.allTime));
			voiceSearchBean = new VoiceSearchBean("", "", -1, -1+"");
			
			break;
		default:
			break;
		}


	}

 

	

}
